<?php
/**
 * Created by PhpStorm.
 * User: 图图
 * Date: 2021-6-8
 * Time: 16:29
 */

namespace app\web\controller;
use app\common\controller\Api;
use app\common\model\Factor;
use app\common\model\Fenorder;
use app\web\controller\Express;
use think\Db;

/**
 * 挂账公司控制器
 * @package app\index\controller
 */
class ChargeCompany extends Auth
{
    public $noNeedLogin = [];

    /**
     *获取挂账单位
     * @return array
     */
    public function lists()
    {
        if ($this->request->isPost()) {
            $userInfo = $this->userInfo;
            $limit = $this->request->post('limit')?? 10;
            $data = Db::name('qt_charge_company')
                ->field('c.*,u.real_name')
                ->alias('c')
                ->leftJoin('qt_users u','u.id = c.user_id')
                ->where(['c.store_id' => $userInfo['store_id']])
                ->order('c.id desc')
                ->paginate($limit);
            $this->result('查询成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 添加挂账单位
     */
    public function add(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['company_name']) || empty($getData['company_address'])){
                $this->result('缺少参数', '', 204);
            }
            if(!isset($getData['content'])){
                $this->result('缺少备注字段', '', 204);
            }
            $insert = [
                'company_name'=>$getData['company_name'],
                'company_address'=>$getData['company_address'],
                'content'=>$getData['content'],
                'user_id'=>$this->userInfo['id'],
                'store_id'=>$this->userInfo['store_id'],
                'create_time'=>time(),
            ];
            $insert = Db::name('qt_charge_company')->insert($insert);
            if($insert){
                $this->result('添加成功', '', 200);
            }
            $this->result('添加失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }

    /**
     * 编辑挂账单位
     */
    public function save(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['company_name']) || empty($getData['company_address'])){
                $this->result('缺少参数', '', 204);
            }
            if(!isset($getData['content'])){
                $this->result('缺少备注字段', '', 204);
            }
            unset($getData['create_time']);
            $update= Db::name('qt_charge_company')->where('id',$getData['id'])->update($getData);
            if($update !== false){
                $this->result('修改成功', '', 200);
            }
            $this->result('修改失败', '', 204);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 编辑挂账单位
     */
    public function edit(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data= Db::name('qt_charge_company')->where('id',$getData['id'])->find();
            $this->result('保存成功', $data, 200);
        }
        $this->result('无效的请求', '', 204);
    }


    /**
     * 删除挂账单位
     */
    public function delete(){
        if ($this->request->isPost()) {
            $getData= $this->request->post();
            if(empty($getData['id'])){
                $this->result('缺少参数', '', 204);
            }
            $data= Db::name('qt_charge_company')->where('id',$getData['id'])->delete();
            $this->result('删除成功', '', 200);
        }
        $this->result('无效的请求', '', 204);
    }


}
